<?php
class Mtpl{
    private $文件地址;
	private $开始标识符;
	private $结束标识符;
	private $缓存时间;
    private $缓存目录;
    private $模板目录;
    private $样式;

    private $T_P = array();
    private $T_R = array();

    public function __construct($config){
        $this->开始标识符=$config['tplstart'];
        $this->结束标识符=$config['tplend'];	
        $this->缓存时间=$config['tplcachetime'];	
        $this->缓存目录=$config['tplcachedir'];
        $this->模板目录=$config['tpldir'];	
        $this->样式=$config['type'];
        //变量输出
        $this->T_P[] = '/'.$this->开始标识符.'\s*?\$(.*?)\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?=\s*?(.+?)\s*?'.$this->结束标识符.'/';
        //判断语句
        $this->T_P[] = '/'.$this->开始标识符.'\s*?if\s*?(.*?)\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?elif\s*?(.*?)\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?else\s*?(.*?)\s*?'.$this->结束标识符.'/';

        //循环语句
        $this->T_P[] = '/'.$this->开始标识符.'\s*?while\s*?(.+?)\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?for\s*?(.*?)\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s?loop\s*?(.+?),(.+?)\s*?in\s*?(.+?)\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s?loop\s*?(.+?)\s*?in\s*?(.+?)\s*?'.$this->结束标识符.'/';

        //原样执行
        $this->T_P[] = '/'.$this->开始标识符.'\s*?:\s*?(.+?)\s*?'.$this->结束标识符.'/';

        //其他语句
        $this->T_P[] = '/'.$this->开始标识符.'\s*?end\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?\/if\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?\/while\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?\/loop\s*?'.$this->结束标识符.'/';
        $this->T_P[] = '/'.$this->开始标识符.'\s*?\/for\s*?'.$this->结束标识符.'/';
        
        //未匹配输出
        $this->T_P[] = '/'.$this->开始标识符.'\s*?(.+?)\s*?'.$this->结束标识符.'/';
        
        //变量输出
        $this->T_R[] = '<?php echo $$1; ?>';
        $this->T_R[] = '<?= $1 ?>';
        
        //判断语句
        $this->T_R[] = '<?php if($1){ ?>';
        $this->T_R[] = '<?php }elseif($1){ ?>';
        $this->T_R[] = '<?php }else{ ?>';

        //循环语句
        $this->T_R[] = '<?php while$1{ ?>';
        $this->T_R[] = '<?php for($1){ ?>';
        $this->T_R[] = '<?php foreach($3 as $1 => $2){ ?>';
        $this->T_R[] = '<?php foreach($2 as $1){ ?>';

        //原样执行
        $this->T_R[] = '<?php $1 ?>';

        //其他语句
        $this->T_R[] = '<?php } ?>';
        $this->T_R[] = '<?php } ?>';
        $this->T_R[] = '<?php } ?>';
        $this->T_R[] = '<?php } ?>';
        $this->T_R[] = '<?php } ?>';
        //未匹配输出
        $this->T_R[] = '<?= $1 ?>';

    }
    public function geturl($tplName){
        if($this->样式 == 'file'){
			$tplName = is_null($tplName) ? (M_PATH ? M_PATH.'_'.M_C.'_'.M_M.'.php' : M_C.'_'.M_M.'.php'):$tplName.'.php';
		}else{
			$tplName = is_null($tplName) ? (M_PATH ? M_PATH.DIRECTORY_SEPARATOR.M_C.DIRECTORY_SEPARATOR.M_M.'.php' : M_C.DIRECTORY_SEPARATOR.M_M.'.php'):$tplName.'.php';
        }
        $真实文件地址=$this->模板目录.$tplName;
        $缓存地址=$this->缓存目录.$tplName;

        if($this->缓存时间<0) {return $真实文件地址;}
        $cachefiletime=@filemtime($缓存地址);
        if(($cachefiletime+$this->缓存时间)>time()) {
            Return $缓存地址;
        }

        $content=$this->解析($真实文件地址);
        if($content===false) {
            $content='template not found:'.$tplName;
        }
        //生成文件
        if(!is_dir(dirname($缓存地址))) {
            $this->createDir(dirname($缓存地址));
        }
        $setCachefile=file_put_contents($缓存地址,$content);
        if($setCachefile) {
            Return $缓存地址;
        }else{
            Return $真实文件地址;
        }

    }
    public function createDir($path){
        if (!file_exists($path)){
            $this->createDir(dirname($path));
            if(!@mkdir($path, 0777)) {
                die(dirname($path).' permission denied');
            }
        }
    }
    public function 解析($文件地址){
        $templatecontent=@file_get_contents($文件地址);
	    if($templatecontent===false) {Return false;}
        //先匹配加载的文件
        $解析过的文件=[];
        $import_pattern = '/'.$this->开始标识符.'\s*file\s*(.+?)\s*'.$this->结束标识符.'/';
        preg_match_all($import_pattern,$templatecontent,$result,PREG_SET_ORDER);
        foreach($result as $file){
            if(!in_array($file[1],$解析过的文件)){
                $templatecontent = str_replace($file[0], "<?php include '{$this->geturl($file[1])}' ?>",$templatecontent);
                $解析过的文件[]=$file[1];
            }
            
        }

        //匹配
        return preg_replace($this->T_P,$this->T_R,$templatecontent);
    }
}